//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')

const demo4Data = [
  1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
]

Pager({
  data: {
    navbarData: {
      height: app.globalData.navheight,
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '列表展示', //导航栏 中间的标题
      bg: 'rgb(255 255 255 / 0%)',
      // opacity: 0,
      position: 'fixed'
      // url: '/pages/index/index'
    },
    height: app.globalData.navheight,
    head: Pager.item({
      title: '列表展示',
      itemClass: 'boxer-custom-nav',
      itemStyle: `--height: ${app.globalData.navheight}px; --paddingTop: ${app.globalData.statusBarHeight}px`
    }),
    demo1: Pager.list({
      data: [
        { title: '全部订单', itemClass: 'column-icon lg-icon icon-order', tap: 'onOrder?id=0', 'hover-class': 'ss-hover-c'},
        { title: '待付款', itemClass: 'column-icon lg-icon icon-pay', tap: 'onOrder?id=1', 'hover-class': 'ss-hover-c'},
        { title: '待收贷', itemClass: 'column-icon lg-icon icon-receiving', tap: 'onOrder?id=2', 'hover-class': 'ss-hover-c'},
        { title: '退款/售后', itemClass: 'column-icon lg-icon icon-aftersale', tap: 'onOrder?id=3', 'hover-class': 'ss-hover-c'},
      ],
      listClass: 'ss-card p-tb flex-row size-sm',
      itemClass: 'flex-1'
    }),
    demo2: Pager.list({
      data: [
        {
          img: {
            src: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1707371206,3427308196&fm=26&gp=0.jpg',
            itemClass: 'mb-minor br pic'
          },
          dot: [
            {title: '澳门新濠影汇酒店的奢豪体验', itemClass: 'title-primary'}
          ],
          itemClass: 'ss-card mt-default'
        },
      ]
    }),
    demo3: Pager.list({
      data: demo3Adapater([1,2,3,4]),
      itemClass: 'click-item'
    }),
    demo4: {
      $$id: 'demo4',
      data: demo4Adapater(demo4Data.slice(0,5)),
      itemClass: 'mt-default',
      footer: {
        $$id: 'loading',
        title: '正在加载中，请稍候...',
        itemClass: 'ss-center color-dot size-sm mt-default'
      }
    }
  },
  kkk: true,
  $demo3: null,
  bindscrolltoupper(e) {
    this.setData({
      'navbarData.bg': `rgb(255 255 255 / 0)`
    })
    this.kkk = true
  },
  updatenavBar(top) {
    const that = this
    if (top > 0 && this.kkk) {
      this.setData({
        'navbarData.bg': `rgb(255 255 255 / 100%)`
      }, () => {
        that.kkk = false
      })
    }
    if (top <= 0) {
      this.setData({
        'navbarData.bg': `rgb(255 255 255 / 0)`
      })
      this.kkk = true
    }
  },
  abc: false,
  bbc: false,
  curIdx: 1,
  onJump(e) {
    wx.navigateTo({
      url: '../detail/index?from=list'
    })
  },
  onPageScroll(e){
    this.updatenavBar(e.scrollTop)
  },
  onReachBottom(e) {
    if (this.curIdx < (demo4Data.length / 5 - 1) ) {
      this.curIdx ++
      const data = demo4Data.slice(5 * (this.curIdx - 1), (5 * this.curIdx))
      this.$demo4.append(demo4Adapater(data))
    }
    else {
      const $loading = this.getElementsById('loading')
      $loading.update({title: '已全部加载'})
    }
  },
  onClick(e, params, inst) {
    const top = e.currentTarget.offsetTop
    this.setData({'scrollTop': top})
    if (inst.hasClass('active a-icon')) {
      instNaNpxoveClass('active a-icon')
    }
    else {
      inst.addClass('active a-icon')
    }
  },
  onShow: function () {},
  onReady: function () {
    this.$demo3 = this.getElementsById('demo3')
    this.$demo4 = this.getElementsById('demo4')
  },
  onLoad: function () {},
})

function demo3Adapater (data) {
  return data.map((item, ii) => {
    return {
      title: [
        ii < 3 ? 
        {
          '@item': {
            img: {
              src: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1707371206,3427308196&fm=26&gp=0.jpg',
              itemClass: 'item-pic',
              mode: 'scaleToFill'
            },
            body: [
              {title: '明星汇经典双床客', itemClass: 'title-primary no-lh-x icon-after-toggle fw-bold',  aim: 'onJump'},
              {title: '42m² 有窗 禁烟', itemClass: 'size-sm no-lh mt-minor mb10'},
              {title: '大床 1.8米', itemClass: 'size-sm no-lh'},
            ],
            footer: [
              {title: [{title: '￥', itemClass: 'size-title color-fail'}, {title: '520', itemClass: 'size-title-lg color-fail'}, {title: '起', itemClass: 'size-form color-fail'}, {title: '(均价)'},{ title: ' ', itemClass: 'icon-toggle-down-c icon-width color-placeholder' }], titleClass: 'flex-row-start-center'},
              {title: '另付税/费¥71', itemClass: 'mr-default no-lh size-sm'}
            ],
            bodyClass: 'flex-1',
            footerClass: 'size-sm item-footer mtb-auto color-info',
            itemClass: 'card-list-item mlr-default head color-default ss-relative',
            // dot: [{title: '爆款', itemClass: 'icon-trapezoid'}]
          }
        }
        : 
        {
          '@item': {
            img: {
              src: 'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1707371206,3427308196&fm=26&gp=0.jpg',
              itemClass: 'item-pic',
              mode: 'scaleToFill'
            },
            body: [
              {title: [{title: '明星汇经典双床客明星汇经典双床客 房经典双床客房', itemClass: 'textellipsis-1 title-primary no-lh-x fw-bold'}, { title: ' ', itemClass: 'icon-toggle-down-c icon-width color-placeholder ml-minor' }], titleClass: 'flex-row-between mb-minor',  aim: 'onJump'},
              {
                title: [
                  {title: [{title: '5.0', itemClass: 'ss-tag tag-fail plain'},{title: '提前2天预订', itemClass: 'ss-tag tag-fail plain ml10'}], titleClass:'flex-row mb-minor'},
                  {title: [{title: '￥', itemClass: 'size-title color-fail'}, {title: '520', itemClass: 'size-title-lg color-fail'}, {title: '起', itemClass: 'size-form color-fail'}], titleClass: 'flex-row-start-center'},
                ],
                titleClass: 'flex-row-between'
              },
              {title: '另付税/费¥71', itemClass: 'flex-row-end size-sm color-info no-lh'}
            ],
            bodyClass: 'flex-1',
            itemClass: 'card-list-item mlr-default head color-default'
          }
        }
      ],
      body: [
        {
          title: [
            {title: '无早 大床 可住2人', itemClass: 'size-form no-lh mb-minor color-title'},
            {title: [{title: '提前2天预订', itemClass: 'ss-tag tag-fail plain'},{title: '提前2天预订', itemClass: 'ss-tag tag-fail plain ml10'},], titleClass:'flex-row mb-minor'},
            {title: '立即确认 12月30日18:00前免费取消', itemClass: 'size-sm no-lh'},
          ],
          footer: [
            {title: [{title: '￥', itemClass: 'size-form color-fail'}, {title: '520', itemClass: 'size-title-sm color-fail'}, {title: '起', itemClass: 'size-form color-fail'}, {title: '(均价)'}], titleClass: 'flex-row-start-center'},
            {title: '另付税/费¥71', itemClass: 'lh40'},
            {title: '预定', itemClass: 'ss-button btn-default small mt5'}
          ],
          titleClass: 'flex-1',
          footerClass: 'size-sm item-footer',
          itemClass: 'card-list-item mlr-default al-item-center'
        },
        {
          title: [
            {title: '无早 大床 可住2人', itemClass: 'size-form no-lh mb-minor color-title'},
            {title: [{title: '提前2天预订', itemClass: 'ss-tag tag-fail plain'},{title: '提前2天预订', itemClass: 'ss-tag tag-fail plain ml10'},], titleClass:'flex-row mb-minor'},
            {title: '立即确认 12月30日18:00前免费取消', itemClass: 'size-sm no-lh'},
          ],
          footer: [
            {title: [{title: '￥', itemClass: 'size-form color-fail'}, {title: '520', itemClass: 'size-title-sm color-fail'}, {title: '起', itemClass: 'size-form color-fail'}, {title: '(均价)'}], titleClass: 'flex-row-start-center'},
            {title: '另付税/费¥71', itemClass: 'lh40'},
            {title: '预定', itemClass: 'ss-button btn-default small mt5'}
          ],
          titleClass: 'flex-1',
          footerClass: 'size-sm item-footer',
          itemClass: 'card-list-item mlr-default al-item-center'
        },
      ],
      bodyClass: 'demo-item-body color-default',
      aim: 'onClick',
      itemClass: ii === 2 ? 'color-disabled' : '',
      titleClass: 'bg-fff ss-sticky',
    }
  })
}

function demo4Adapater (data) {
  return data.map((item, ii) => {
    return {
      body: [
        {title: '2022_'+item+'期', itemClass: 'title-primary no-lh mb-minor'},
        {title: '中奖奖品', itemClass: 'size-sm fw-bold color-fail no-lh mb5'},
        {title: '澳门新濠天地-摩珀斯 豪华套房一澳门新濠天地-摩珀斯 豪华套房一...', itemClass: 'size-sm fw-bold color-title textellipsis-1'},
        {title: '兑奖有效期: 2021年1月11日-2021年2月11日', itemClass: 'size20 color-info no-lh mt15'},
      ],
      footer: [
        {title: '去兑奖', itemClass: 'ss-button btn-default small'}
      ],
      bodyClass: 'item-body',
      footerClass: 'item-footer',
      itemClass: 'coupon-item'
    }
  })
}

function throttle(fn, interval) {
  //节流
  var enterTime = 0;//触发的时间
  var gapTime = interval || 100 ;//间隔时间，如果interval不传，则默认300ms
  return function() {
    var context = this;
    var backTime = new Date();//第一次函数return即触发的时间
    if (backTime - enterTime > gapTime) {
      fn.call(context,arguments);
      enterTime = backTime;//赋值给第一次触发的时间，这样就保存了第二次触发的时间
    }
  };
}
function debounce(fn,interval){
  //防抖
  var timer;
  var gapTime = interval || 200;//间隔时间，如果interval不传，则默认1000ms
  return function() {
    clearTimeout(timer);
    var context = this;
    var args = arguments;//保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
    timer = setTimeout(function() {
      fn.call(context,args);
    }, gapTime);
  };
}
